package br.com.toscano.contpedido.view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.primefaces.event.FileUploadEvent;

import br.com.toscano.contpedido.business.ClienteBC;
import br.com.toscano.contpedido.domain.Cliente;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractPageBean;

@ViewController
@PreviousView("./cliente_importar_planilha.jsf")
public class ClienteImportarPlanilhaMB extends AbstractPageBean {

	private static final long serialVersionUID = 1L;

	@Inject
	private ClienteBC clienteBC;

	private String separacao;

	public String getSeparacao() {
		if (separacao == null) separacao = ";";
		return separacao;
	}

	public void setSeparacao(String separacao) {
		this.separacao = separacao;
	}

	@Inject
	private FacesContext facesContext;

	public String insert() throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(file.getFile().getInputstream()));
		
		List<Cliente> list = clienteBC.importarPlanilha(separacao, in);
	
		this.file = null;
		facesContext.addMessage("file", new FacesMessage("Importado com Sucesso!"));
		
		return this.getCurrentView();
	}

	private FileUploadEvent file;
	
	public void handleFileUpload(FileUploadEvent event) {
		this.file = event;
	}

	public FileUploadEvent getFile() {
		return file;
	}

	public void setFile(FileUploadEvent file) {
		this.file = file;
	}
	public void setarSeparacao() {
		
	}
}